<?php 
namespace lib\protocol;
class WxIpad{
    protected $api;
    protected $Header = [];
    public function __construct($api = null){
        $this->api = empty($api) ? 'http://pc.wfkc.tech:778':$api;
        $this->Header = ['Content-Type: application/json-patch+json'];
    }
    public function wxlogin($DeviceID = ''){
        unset($_SESSION['wx_data62']);
        $api = $this->api . '/Login/GetQR';
        $data = [
            'DeviceID'=>$DeviceID?:'',
            'DeviceName'=>'',
            'OSModel'=>'',
            'Proxy'=>[
                'ProxyIp'=>'',
                'ProxyPassword'=>'',
                'ProxyUser'=>''
            ],
        ];
        $res=$this->http_post_yun($api,$this->Header,$data);
        $res=json_decode($res,true);
        
        if($res['Code'] == 1){
            $_SESSION['wx_data62'] = $res['Data62'];
            return ["uuid" => $res["Data"]["Uuid"], "id" => $res['DeviceId'], "qr_url" =>$res["Data"]["QrBase64"]];
        }
        return ['code'=>-1,'msg'=>'未知错误'];
    }
    public  function wxCheckLoginQrcode($guid, $uuid){
        unset($_SESSION['wx_code_data']);
        $api = $this->api . '/Login/CheckQR?uuid='.$uuid;
        $res = $this->http_post_yun($api,$this->Header,[]);
        $res=json_decode($res,true);
        
        if($res['Message'] == '登陆成功' && $res['Data']['acctSectResp']['userName']){
            return ["code" => 0, "msg" => "登录成功", "cookie" => $res['Data']['acctSectResp']['userName']];
        }
        if(is_array($res['Data'])){
            if($res['Data']['status'] == 0){
                $result = ["code" => 1, "msg" => "等待扫码中", "state" => $res["Data"]["status"]];
            }else if($res['Data']['status'] == 1){
                $result = ["code" => 2, "msg" => "等待确认中", "state" => $res["Data"]["status"]];
            }
        }
        if($res['Code'] == -3){
            $_SESSION['wx_code_data'] = $res['Data'];
            return ['code'=>-3 , 'msg'=>$res['Message']];
        }
        if(empty($result)){
            $result = ["code" => -1, "msg" => "错误状态", "state" => $res["Data"]["status"]];
        }
        return $result;
    }
    public function WXHeartBeat($wxid){
        $api = $this->api . '/Login/HeartBeat?wxid='.$wxid;
        $res=$this->http_post_yun($api,$this->Header,[]);
        $res=json_decode($res,true);
        if($res['Data'] && $res['Code'] == 0){
    	 	 $result = ["code" => 1, "msg" => "心跳正常"];
	 	}else{
	 	     $result = ["code" => -1, "msg" => "账户已经离线"];
	 	}
	 	return $result;
    }
    public function VerifyCode($uuid  , $code){
        if(empty($_SESSION['wx_data62']) || empty($_SESSION['wx_code_data'])){
            return ['code'=>-1,'msg'=>'数据不存在'];
        }
        $api = $this->api . '/Login/VerifyCode';
        $data = [
            'Uuid'=>$uuid,
            'Data62'=>$_SESSION['wx_data62'],
            'Code'=>$code,
            'Data'=>$_SESSION['wx_code_data']
        ];
        $res = $this->http_post_yun($api,$this->Header,$data);
        $res = json_decode($res,true);
        if($res['code'] == 0 && $res['Success'] == true){
            unset($_SESSION['wx_data62']);
            unset($_SESSION['wx_code_data']);
            return ['code'=>200,'msg'=>'success'];
        }
        return ['code'=>-1,'msg'=>$res['Message']];
    }
    public function Order($wxid){
        $api = $this->api . '/Msg/Sync';
        $data = [
            'Scene'=>0,
            'Synckey'=>'string',
            'Wxid'=>$wxid
        ];
        $res    = $this->http_post_yun($api,$this->Header,$data);
        $result = $this->stripslashes_deep($res);
        return $result;
    }
    public function WXF2FQrcode($wxid , $money , $Description){
        $api = $this->api . '/TenPay/GMPayQCode';
        $data = [
            'Name'=>$Description,
            'Money'=>(string)($money * 100),
            'Wxid'=>$wxid
        ];
        
        $res = $this->http_post_yun($api,$this->Header,$data);
        
        $res = json_decode($res, true); 
        
        if($res['Data']['retText']['buffer']){
            $res  = base64_decode($res['Data']['retText']['buffer']);
            $arrs = $this->stripslashes_deep($res);
            return $arrs; 
        }
        return false;
    }
    public function WXJYQrcode($wxid , $money , $Description , $beizhu){
        $api = $this->api . '/TenPay/GMJyPayQCode';
        $data = [
            'Name'=>$Description,
            'Money'=>$money,
            'Remark'=>$beizhu,
            'Wxid'=>$wxid
        ];
        $res = $this->http_post_yun($api,$this->Header,$data);
        $arrs = $this->stripslashes_deep($res);
        return $arrs;
    }
    public function WXJYQrcodeUrl($wxid , $money , $Description , $beizhu){
        $api = $this->api . '/TenPay/GMJyPayQCodeUrl';
        $data = [
            'Name'=>'在线收款',
            'Money'=>$money,
            'Remark'=>$beizhu,
            'Wxid'=>$wxid
        ];
       
        $res = $this->http_post_yun($api,$this->Header,$data);
        $arrs = $this->stripslashes_deep($res);
        return $arrs;
    }
    protected function stripslashes_deep($value) {
        
        $value = is_array($value) ?
            array_map([$this, 'stripslashes_deep'], $value) : // 使用 $this 引用类方法
            stripslashes($value);
        return $value;
    }
    protected function http_post_yun($url, $Header, $data=NULL){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $Header);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POST, 1); // 发送一个常规的Post请求
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); // Post提交的数据包
        curl_setopt($ch, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
        curl_setopt($ch, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
        $sResult = curl_exec($ch);
        if($sError=curl_error($ch)){
            die($sError);
        }
        curl_close($ch);
        return $sResult;
    }
}
?>